<?php

namespace Admin\Controller;

/**
 * 
 * 设计师的作品管理
 */
class WorksController extends AdminController {

    private $m;

    public function _initialize() {
        parent::_initialize();
        //查询出所有的店铺
        $store = M("store")->field("id,sname")->select();
        $this->assign("store",$store);
        $this->assign("meta_title", "作品管理");
        $this->m = D("Works");
    }
    
     //查询设计师
    public function getDesigner(){
        $sid = I("sid",0,'intval');
        $where = array();
        if($sid!=0){
            $where["sid"] = array("eq",$sid);
        }
        $designer = M("designer")->field("id,dname")->where($where)->select();
      
        if(IS_AJAX){ 
            exit(json_encode($designer));
        }
        return $designer;
    }

    public function index() {
        $where = array();
        $keyword = I("keyword",'','strip_tags');
        if(!empty($keyword)&&$keyword!=''){
            $where["wname"]=array('like','%'.$keyword.'%');
        }
        $list = $this->lists($this->m->relation(true), $where);
        foreach ($list as $key => $value) {
            $map["id"] = array("eq",$value["sid"]);
            $list[$key]["sname"] = M("store")->where($map)->getField("sname");
        }
        $this->assign("list", $list);
        $this->display();
    }

    public function add() {
        if (IS_POST) {
            $_POST['pic'] = implode(',',$_POST['pic']);
            $this->m->create(I("post."));
            $result = $this->m->add();
            if ($result) {
                $this->success("操作成功！", U('index'));
            } else {
                $this->error($this->m->getError());
            }
        } else {
            $this->display();
        }
    }

    public function edit($id) {
        if (IS_POST) {
            $_POST['pic'] = implode(',',$_POST['pic']);
            $this->m->create(I("post."));
            $where["id"] = array("eq", I("id"));
            $result = $this->m->where($where)->save();
            if ($result) {
                $this->success("操作成功！", U('index'));
            } else {
                $this->error($this->m->getError());
            }
        } else {
            $info = $this->m->relation(true)->find($id);
            //查询出该门店下面的设计师
            $designer=M("designer")->where(array("id"=>array("eq",$info["did"])))->select();
            $this->assign("designer",$designer);
            if( $info['pic']!=null){
                $info['pic'] = explode(',',$info['pic']);
            }
            $this->assign("info", $info);
            $this->display("add");
        }
    }

    public function del() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));
        if ($this->m->where($map)->delete()) {
            // S('DB_CONFIG_DATA',null);
            //记录行为
            action_log('delete_store', 'Store', $id, UID);
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

}
